/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package	dao;

import Hibernate.HibernateUtil;
import pojo.Author;
import pojo.Book;
import pojo.BookRelative;
import pojo.Comment;
import pojo.User;



import java.util.ArrayList;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class BookRelativeDAO extends AbstractDAO {

	@Override
	protected Class getPOJOClass() {
		// TODO Auto-generated method stub
		return BookRelative.class;
	}
	
    public BookRelative getBookRelativeByID(int id){
        BookRelative bookRelative = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            bookRelative = (BookRelative)session.get(BookRelative.class, id);
        }
        catch(HibernateException ex){
            
        }
        finally{
            session.close();
        }
        return bookRelative;
    }
    
    public boolean insertOrUpdate(Book b1, Book b2)
    {
    	if(b1.getBookId()==b2.getBookId()) return false;
    	boolean flag=false;
    	List<BookRelative> ds = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select br from BookRelative br where br.bookByBookId.bookId=:n1 and br.bookByBookRelativeId.bookId =:n2";
		Query query = (Query) session.createQuery(sql).setParameter("n1",b1.getBookId()).setParameter("n2",b2.getBookId());
		ds = query.list();
		Transaction transaction = null;
		if (ds.size() > 0)
		{
			BookRelative br= ds.get(0);
			int n= br.getView()+1;
			br.setView(n);
			try {
				transaction = session.beginTransaction();
				session.update(br);
				session.flush();
				transaction.commit();
				flag = true;
				ds = query.list();
			} catch (HibernateException ex) {
				System.out.println(ex);
			} finally {
				session.close();
			}
		}
		else
		{
			List<BookRelative> l= this.getList();    	
			BookRelative br= new BookRelative();
			br.setBookByBookId(b1);
			br.setBookByBookRelativeId(b2);
			br.setView(1);
			try {
				transaction = session.beginTransaction();
				session.save(br);
				session.flush();
				transaction.commit();
				flag = true;
			} catch (HibernateException ex) {
				System.out.println(ex);
			} finally {
				session.close();
			}
		}
    	return flag;
    }
    
    public List<Book> getNBookShouldView(Book b,int n)
    {
    	List<Book> ds=null;
    	List<BookRelative> ds2;
    	Session session = HibernateUtil.getSessionFactory().openSession();
    	String sql = "select br from BookRelative br where br.bookByBookId.bookId = :n1 and br.view > 20 order by br.view desc ";
		Query query = (Query) session.createQuery(sql).setParameter("n1",b.getBookId());
		query.setMaxResults(n);
		ds2 = query.list();
    	if(ds2.size()>0)
    	{
    		ds= new ArrayList<Book>();
    		BookDAO bd= new BookDAO();
    		for(int i= 0; i< ds2.size();i++)
    		{
    			BookRelative br = ds2.get(i);
    			ds.add(br.getBookByBookRelativeId());
    		}

    	}
    	else
    	{
    		return null;
    		/*
    		sql = "select s from Book s where s.topic.topicId =:n order by s.view desc ";
    		query = (Query) session.createQuery(sql).setParameter("n",b.getTopic().getTopicId());
    		query.setMaxResults(n);
    		ds=query.list();
    		*/
    	}
    	return ds;
    }
    
}
